package tuan.core.extract;

import java.io.*; 

import org.cyberneko.html.parsers.DOMParser;
import org.w3c.dom.Node;

public class Demo {
	static String[] getProductUrl() {
		return new String[] { "http://tuan.aibang.com/beijing/aiyihang.html", };
	}

	static String[] getProductListUrl() {
		return new String[] { "http://tuan.aibang.com/beijing/aiyihang.html", };
	}

	public static void getProductDetail(Product product) throws Exception {
		DOMParser parser = new DOMParser();
		parser.setProperty("http://cyberneko.org/html/properties/default-encoding", getEncoding(product.url));
		parser.parse(product.url);
		Node root = parser.getDocument();
		ProductExtract pe = new ProductExtract(product, root);
		pe.extract();
		System.out.println(product.fetchOk + ":" + product.url);
	}

	static String getEncoding(String url) {
		if (url.indexOf("ju.taobao.com") >= 0) {
			return "gbk";
		}
		if (url.indexOf("cocotuan.com") >= 0) {
			return "utf-8";
		}
		if (url.indexOf("tuanju365.com") >= 0) {
			return "utf-8";
		}
		return "gbk";
	}

	public static void main(String[] args) throws Exception {
		Product product = new Product();
		product.url = args[0];
		getProductDetail(product);
		System.out.println(product.toString());
	}

	static void writeToFile(String fileName, String text) {
		try {
			File outFile = new File(fileName);
			FileWriter writer = new FileWriter(outFile);
			writer.write(text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
